package com.base.api.service.selfAddress.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.api.dto.selfAddress.SelfAddressApiAddDTO;
import com.base.api.dto.selfAddress.SelfAddressApiDelDTO;
import com.base.api.dto.selfAddress.SelfAddressApiListDTO;
import com.base.api.service.selfAddress.SelfAddressApiService;
import com.base.mapper.voipmapper.SubscribersMapper;
import com.base.po.voip.Subscribers;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.entity.SelfAddress;
import com.base.self.mapper.SelfAddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Optional;

@Service
public class SelfAddressApiServiceImpl implements SelfAddressApiService {

    private final SelfAddressMapper selfAddressMapper;

    private final SubscribersMapper subscribersMapper;

    public SelfAddressApiServiceImpl(SelfAddressMapper selfAddressMapper, SubscribersMapper subscribersMapper) {
        this.selfAddressMapper = selfAddressMapper;
        this.subscribersMapper = subscribersMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult selfAddressApiList(SelfAddressApiListDTO selfAddressApiListDTO) {
        final Map<String, Object> data = PageUtils.getDateMap(() ->
                        selfAddressMapper.selectPage(
                                PageUtils.getPage(
                                        new Page<>(),
                                        selfAddressApiListDTO.getOffset(),
                                        selfAddressApiListDTO.getLimit()
                                ),
                                new QueryWrapper<SelfAddress>()
                                        .eq("is_del", 0)
                                        .eq("user_id", selfAddressApiListDTO.getUserId())
                                        .orderByDesc("create_time")
                        ));
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }

    @Override
    @Transactional
    public ResponseResult selfAddressApiAdd(SelfAddressApiAddDTO selfAddressApiAddDTO) {
        final Subscribers subscribers = subscribersMapper.selectByPrimaryKey(selfAddressApiAddDTO.getUserId());
        Optional.ofNullable(subscribers).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("用户不存在")));
        final SelfAddress selfAddress = SelfAddress
                .builder()
                .province(selfAddressApiAddDTO.getProvince())
                .city(selfAddressApiAddDTO.getCity())
                .district(selfAddressApiAddDTO.getDistrict())
                .address(selfAddressApiAddDTO.getAddress())
                .createTime(DateUtil.date())
                .mobile(selfAddressApiAddDTO.getMobile())
                .userId(subscribers.getId())
                .name(selfAddressApiAddDTO.getName())
                .userMobile(subscribers.getMobile())
                .build();
        selfAddressMapper.insert(selfAddress);
        return ResponseResultUtils.getResponseResultS("添加成功");
    }

    @Override
    @Transactional
    public ResponseResult selfAddressApiDel(SelfAddressApiDelDTO selfAddressApiDelDTO) {
        final Subscribers subscribers = subscribersMapper.selectByPrimaryKey(selfAddressApiDelDTO.getUserId());
        Optional.ofNullable(subscribers).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("用户不存在")));

        final SelfAddress selfAddress = selfAddressMapper.selectById(selfAddressApiDelDTO.getId());
        Optional.ofNullable(selfAddress).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("收货地址错误")));

        selfAddress.setIsDel(1);
        selfAddress.setUpdateTime(DateUtil.date());
        selfAddressMapper.updateById(selfAddress);
        return ResponseResultUtils.getResponseResultS("删除成功");
    }
}
